//定义js命名空间
Ext.namespace('FSBS.FsbsQmCrif');

//FsbsQmCrif FieldArray
FSBS.FsbsQmCrif.BaseFieldsArray = new Array(
	{ name: 'Id', type: 'string' },
	{ name: 'IccardId', type: 'string' },
	{ name: 'Iccarduid', type: 'string' },
	{ name: 'KeyIccardId', type: 'string' },
	{ name: 'KeyIccarduid', type: 'string' },
	{ name: 'DepartmentId', type: 'string' },
	{ name: 'Brand', type: 'string' },
	{ name: 'Series', type: 'string' },
	{ name: 'Model', type: 'string' },
	{ name: 'Color', type: 'string' },
	{ name: 'EnginId', type: 'string' },
	{ name: 'FrameId', type: 'string' },
	{ name: 'ProdDate', type: 'date' },
	{ name: 'Mileage', type: 'float' },
	{ name: 'MarketPrice', type: 'float' },
	{ name: 'StoreIndt', type: 'date' },
	{ name: 'StoreOutdt', type: 'date' },
	{ name: 'AtmGjb', type: 'int' },
	{ name: 'AtmBt', type: 'int' },
	{ name: 'AtmSms', type: 'int' },
	{ name: 'AtmDyq', type: 'int' },
	{ name: 'AtmBxk', type: 'int' },
	{ name: 'AtmKey', type: 'int' },
	{ name: 'AtmMast', type: 'int' },
	{ name: 'AtmWiper', type: 'int' },
	{ name: 'AtmCertificate', type: 'int' },
	{ name: 'CustId', type: 'string' },
	{ name: 'State', type: 'int' },
	{ name: 'Description', type: 'string' },
	{ name: 'Isdelete', type: 'bool' },
	{ name: 'Version', type: 'int' },
	{ name: 'Createtime', type: 'date' }
);

//FsbsQmCrif JsonReader
FSBS.FsbsQmCrif.JsonReader = Ext.extend(Infrastructure.BaseJsonReader, {
	constructor: function (cfg) {
		cfg = cfg || {};
		FSBS.FsbsQmCrif.JsonReader.superclass.constructor.call(this, Ext.apply({
			fields: FSBS.FsbsQmCrif.BaseFieldsArray
		}, cfg));
	}
});

//FsbsQmCrif JsonStore
FSBS.FsbsQmCrif.JsonStore_old = Ext.extend(Infrastructure.BaseJsonStore, {
	constructor: function (cfg) {
		cfg = cfg || {};
		FSBS.FsbsQmCrif.JsonStore_old.superclass.constructor.call(this, Ext.applyIf({
			QryUrl: '/FsbsQmCrif/GetPlist', //查询Url
			UpdUrl: '/FsbsQmCrif/SaveOrUpdate', //更新记录Url
			DelUrl: '/FsbsQmCrif/Delete', //删除记录Url
			fields: FSBS.FsbsQmCrif.BaseFieldsArray
		}, cfg));
	}
});

FSBS.FsbsQmCrif.JsonStore = Ext.extend(Infrastructure.BaseJsonStore, {
	SelUrl: '/FsbsQmCrif/GetPlist', //查询Url
	UpdUrl: '/FsbsQmCrif/SaveOrUpdate', //更新记录Url
	DelUrl: '/FsbsQmCrif/Delete', //删除记录Url
	fields: FSBS.FsbsQmCrif.BaseFieldsArray
});

//FsbsQmCrif GridPanel
FSBS.FsbsQmCrif.GridPanel = Ext.extend(Infrastructure.BaseGridPanel, {
	// User property
	//基本处理Url
	baseUrl: '/FsbsQmCrif/',
	//提交保存Url
	formSubmitUrl: '/FsbsQmCrif/SaveOrUpdate',
	//提交保存提示信息
	formSubmitMsg: '确定要保存商品车信息吗?',

	//constructor
	initComponent: function () {
		//prepare

		var itemGrid = this;
		//set store params
		var gridStore = new FSBS.FsbsQmCrif.JsonStore();

		// The filter.
		var gridFilter = new Ext.ux.grid.GridFilters({
			encode: true,
			filters: [
				{
					dataIndex: 'Id',
					type: 'string'
				}, {
					dataIndex: 'IccardId',
					type: 'string'
				}, {
					dataIndex: 'Iccarduid',
					type: 'string'
				}, {
					dataIndex: 'KeyIccardId',
					type: 'string'
				}, {
					dataIndex: 'KeyIccarduid',
					type: 'string'
				}, {
					dataIndex: 'DepartmentId',
					type: 'list',
					options: Infrastructure.Params.DepartmentFilter()
				}, {
					dataIndex: 'Brand',
					type: 'list',
					options: Infrastructure.Params.DictFilter('Brand')
				}, {
					dataIndex: 'Series',
					type: 'list',
					options: Infrastructure.Params.DictFilter('Series')
				}, {
					dataIndex: 'Model',
					type: 'list',
					options: Infrastructure.Params.DictFilter('Model')
				}, {
					dataIndex: 'Color',
					type: 'list',
					options: Infrastructure.Params.DictFilter('Color')
				}, {
					dataIndex: 'EnginId',
					type: 'string'
				}, {
					dataIndex: 'FrameId',
					type: 'string'
				}, {
					dataIndex: 'ProdDate',
					type: 'date'
				}, {
					dataIndex: 'Mileage',
					type: 'int'
				}, {
					dataIndex: 'MarketPrice',
					type: 'int'
				}, {
					dataIndex: 'StoreIndt',
					type: 'date'
				}, {
					dataIndex: 'StoreOutdt',
					type: 'date'
				}, {
					dataIndex: 'State',
					type: 'string'
				}, {
					dataIndex: 'Createtime',
					type: 'date'
				}
			]
		});

		var gridSelModel = new Ext.grid.CheckboxSelectionModel({
			//header: '',
			singleSelect: false,
			listeners: {
				'selectionchange': function (sm) {
					// 控制工具栏按钮是否可用
					var bottomToolbar = itemGrid.getBottomToolbar();
					bottomToolbar.btnDel.setDisabled(sm.getCount() < 1);
					bottomToolbar.btnSel.setDisabled(sm.getCount() < 1);
				}
			}
		});

		// GridColumn
		var gridColumn = new Ext.grid.ColumnModel({
			columns: [
				new Ext.grid.RowNumberer(),
			//gridSelModel,
				{
				header: "车辆编号",
				dataIndex: 'Id'
			}, {
				header: "机构",
				dataIndex: 'DepartmentId',
				renderer: Infrastructure.Params.DepartmentRender
			}, {
				header: "品牌",
				dataIndex: 'Brand',
				xtype: 'infra_dictcolumn',
				dictCode: 'Brand'
			}, {
				header: "系列",
				dataIndex: 'Series',
				xtype: 'infra_dictcolumn',
				dictCode: 'Series'
			}, {
				header: "型号",
				dataIndex: 'Model',
				xtype: 'infra_dictcolumn',
				dictCode: 'Model'
			}, {
				header: "颜色",
				dataIndex: 'Color',
				xtype: 'infra_dictcolumn',
				dictCode: 'Color'
			}, {
				header: "发动机号",
				dataIndex: 'EnginId'
			}, {
				header: "车架号",
				dataIndex: 'FrameId'
			}, {
				header: "生产日期",
				dataIndex: 'ProdDate',
				xtype: 'datecolumn',
				format: 'Y-m-d'
			}, {
				header: "里程",
				dataIndex: 'Mileage',
				xtype: 'numbercolumn',
				align: 'right'
			}, {
				header: "市场指导价",
				dataIndex: 'MarketPrice',
				xtype: 'numbercolumn',
				align: 'right'
			}, {
				header: "入库日期",
				dataIndex: 'StoreIndt',
				xtype: 'datecolumn',
				format: 'Y-m-d'
			}, {
				header: "出库日期",
				dataIndex: 'StoreOutdt',
				xtype: 'datecolumn',
				format: 'Y-m-d'
			}, {
				header: "状态",
				dataIndex: 'State',
				xtype: 'infra_KeyValueColumn',
				dataStore: FSBS.Params.CarStateStore
			}
			],
			defaults: {
				sortable: true,
				editor: {
					xtype: 'textfield'
				}
			}
		});

		// 常用工具栏
		var toolBar = [{
			xtype: 'tbaraddbtn', //录入
			menuid: '010101',
			hidden: false,
			handler: function () {
				itemGrid.onShowItemWin();
			}
		}, {
			xtype: 'tbardelbtn', //删除
			hidden: '010101',
			ishidden: false,
			handler: function () {
				itemGrid.delEntity();
			}
		}, {
			xtype: 'tbareditbtn', //编辑
			menuid: '010101',
			hidden: false,
			handler: function () {
				itemGrid.onShowItemWin(itemGrid.getSelectionModel().getSelected());
			}
		}, {
			xtype: 'tbbutton', //打印二维码
			menuid: '010301',
			text: '打印条码',
			ref: '../btnPrintQrCode',
			icon: '/Content/icons/qr_code.png',
			hidden: false,
			handler: function () {
				itemGrid.onPrintQrCode();
			}
		}, '-', {
			xtype: 'textfield',
			width: 150,
			id: 'q_query',
			ref: '../q_query',
			listeners: {
				specialkey: function (field, e) {
					if (e.getKey() == e.ENTER) {
						itemGrid.qryList(field.getValue());
					}
				}
			}
		}, {
			xtype: 'tbarqrybtn',
			handler: function (e) {
				itemGrid.qryList(itemGrid.q_query.getValue());
			},
			scope: itemGrid
		}];

		Ext.applyIf(this, {
			plugins: [gridFilter],
			cm: gridColumn,
			sm: gridSelModel,
			store: gridStore,
			tbar: toolBar,
			listeners: {
				'rowdblclick': function (grid, rowIndex, e) {
					this.onShowItemWin(grid.store.getAt(rowIndex));
				}
			}
		});

		FSBS.FsbsQmCrif.GridPanel.superclass.initComponent.call(this);
	},

	/****************** 
	* user function 
	******************/
	// 添加
	addEntity: function () {
		this.onShowItemWin();
	},

	// 读取信息显示
	onShowItemWin: function (selRecord) {
		var itemGrid = this;
		var dataId = Ext.isDefined(selRecord) ? selRecord.data.Id : '';
		var isEdit = Ext.isDefined(selRecord) ? selRecord.data.State == '0' : true;
		var itemEditForm = new FSBS.FsbsQmCrif.EditForm({
			isEdit: isEdit,
			submitUrl: this.formSubmitUrl,
			dataId: dataId,
			storeId: this.storeId,
			dataRecord: selRecord,
			listeners: {
				'afterSave': function (ele, rt) {
					if (rt) {
						if (!Ext.isDefined(selRecord))
							ele.form.reset();
						itemGrid.store.reload();
						//JsHelper.ShowInfo('信息更新成功!');
					}
				}
			}
		});

		var itemEditWin = new Infrastructure.BaseWindow({
			title: '商品车信息',
			iconCls: 'car',
			width: 600,
			items: [itemEditForm],
			buttons: [new Ext.ux.SubmitBtn({
				menuid: '',
				hidden: !(isEdit && itemEditForm.submitUrl != ''),
				handler: function () {
					itemEditForm.saveItem(itemEditForm.submitUrl)
				},
				scope: itemEditForm
			}), new Ext.ux.CancelBtn({
				handler: function () {
					itemEditForm.ownerCt.destroy();
				}
			})]
		});
		itemEditWin.show();
		if (Ext.isDefined(selRecord)) {
			itemEditForm.form.loadRecord(selRecord);
		}
	},

	//打印二维码
	onPrintQrCode: function () {
		var itemGrid = this;
		var carQrCodeView = new FSBS.FsbsQmCrif.QrCodeDataView();

		var s = itemGrid.getSelectionModel().getSelections();
		carQrCodeView.store.removeAll();
		for (var i = 0, r; r = s[i]; i++) {
			carQrCodeView.store.add(r);
		}

		var panel = new Ext.Panel({
			id: 'images-view',
			frame: true,
			width: 650,
			//height : 600,
			autoHeight: true,
			layout: 'fit',
			header: false,
			items: [carQrCodeView]
		});

		var panel_outer = new Ext.Panel({
			width: 680,
			height: 500,
			autoScroll: true,
			tbar: [{
				xtype: 'tbarprintbtn',
				handler: function () {
					Ext.ux.Printer.print(panel_outer);
				}
			}, '-', {
				xtype: 'btncancle',
				handler: function () {
					itemEditWin.destroy();
				}
			}],
			items: [panel]
		});

		var itemEditWin = new Ext.Window({
			title: '汽贸-车辆二维码打印',
			iconCls: 'car',
			width: 700,
			autoHeight: true,
			resizable: false,
			constrain: true,
			modal: true,
			closeAction: 'close',
			plain: true,
			buttonAlign: 'center',
			items: [panel_outer]
		});
		itemEditWin.show();
	}
});

//FsbsQmCrif EditForm
FSBS.FsbsQmCrif.EditForm = Ext.extend(Infrastructure.BaseForm, {
	storeId: null,
	dataId: null,
	dataRecord: null,
	submitUrl: '/FsbsQmCrif/SaveOrUpdate',
	submitMsg: '确定要保存吗?',

	initComponent: function () {
		var itemEditForm = this;

		//Form第一列
		// 定义表单列
		var firstColumn = [{
			fieldLabel: "车辆ID卡",
			maxLength: 36,
			name: 'Iccarduid'
		}, {
			fieldLabel: "车匙ID卡",
			maxLength: 36,
			name: 'KeyIccarduid'
		}, {
			fieldLabel: "品牌",
			xtype: 'infra_dictcombo',
			filterValue: 'Brand',
			hiddenName: 'Brand'
		}, {
			fieldLabel: "系列",
			xtype: 'infra_dictcombo',
			filterValue: 'Series',
			hiddenName: 'Series'
		}, {
			fieldLabel: "型号",
			xtype: 'infra_dictcombo',
			filterValue: 'Model',
			hiddenName: 'Model'
		}, {
			fieldLabel: "颜色",
			xtype: 'infra_dictcombo',
			filterValue: 'Color',
			hiddenName: 'Color'
		}, {
			fieldLabel: "发动机号",
			maxLength: 30,
			name: 'EnginId'
		}, {
			fieldLabel: "车架号",
			maxLength: 30,
			name: 'FrameId'
		}, {
			fieldLabel: "生产日期",
			xtype: 'datefield',
			format: 'Y-m-d',
			name: 'ProdDate'
		}, {
			fieldLabel: "里程",
			xtype: 'numberfield',
			name: 'Mileage'
		}, {
			fieldLabel: "市场指导价",
			xtype: 'numberfield',
			name: 'MarketPrice'
		}];

		//Form第二列
		var secondColumn = [{
			fieldLabel: '入库日期',
			xtype: 'datefield',
			format: 'Y-m-d',
			name: 'StoreDate'
		}, {
			fieldLabel: '出库日期',
			xtype: 'datefield',
			format: 'Y-m-d',
			name: 'StoreOutdt'
		}, {
			fieldLabel: '展场',
			xtype: 'infra_deptcombo',
			hiddenName: 'DepartmentId'
		}, {
			fieldLabel: '状态',
			xtype: 'infra_localcombo',
			store: FsCarInfo.Params.CarStateStore,
			name: 'State'
		}, {
			fieldLabel: '二维码',
			xtype: 'qrcodeImage',
			src: '/FsbsQmCrif/GetCarQrCodeById?size=150&Id=' + this.dataId
		}];

		// 定义表单单独列的组件对象集合
		var atmCheckboxGroup = new Ext.form.CheckboxGroup({
			autoHeight: true,
			layout: 'column',
			columns: 3,
			xtype: 'checkboxgroup',
			id: 'atmGroup',
			anchor: '100%',
			fieldLabel: '附件',
			//itemCls: 'x-check-group-alt',
			items: [{
				boxLabel: '全选',
				name: 'Atm_All',
				inputValue: '1',
				listeners: {
					'check': function () {
						var group = Ext.getCmp('atmGroup');
						var length = group.items.getCount();
						var all;
						if (this.checked == true) {
							all = true;
						} else {
							all = false;
						}
						for (i = 0; i < length; i++) {
							var colItems = group.items.get(i);
							colItems.setValue(all);
						}
					}
				}
			}, {
				boxLabel: '工具包', name: 'AtmGjb', inputValue: '1'
			}, {
				boxLabel: '车钥匙', name: 'AtmKey', inputValue: '1'
			}, {
				boxLabel: '备胎', name: 'AtmBt', inputValue: '1'
			}, {
				boxLabel: '点烟器', name: 'AtmDyq', inputValue: '1'
			}, {
				boxLabel: '雨刮', name: 'AtmWiper', inputValue: '1'
			}, {
				boxLabel: '天线', name: 'AtmMast', inputValue: '1'
			}, {
				boxLabel: '合格证', name: 'AtmCertificate', inputValue: '1'
			}, {
				boxLabel: '说明书', name: 'AtmSms', inputValue: '1'
			}, {
				boxLabel: '保修卡', name: 'AtmBxk', inputValue: '1'
			}]
		});

		var singleColumn = [atmCheckboxGroup,
		{
			xtype: 'textarea',
			fieldLabel: '描述',
			name: 'Description',
			readOnly: !this.isEdit,
			height: 50,
			width: 430
		}];

		Ext.applyIf(this, {
			items: [{
				layout: 'column',
				// 注意 现在使用的是两列 要是三列或更多
				// 请改变下面的items
				// 并在上面定义每个列的组件集合
				items: [{
					columnWidth: .5,
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					defaults: {
						width: 150,
						allowBlank: false,
						readOnly: !this.isEdit
					},
					items: firstColumn
				}, {
					columnWidth: .5,
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					defaults: {
						width: 150,
						readOnly: true,
						allowBlank: true
					},
					items: secondColumn
				}]
			}, singleColumn],
			reader: new FSBS.FsbsQmCrif.JsonReader()
		});

		FSBS.FsbsQmCrif.EditForm.superclass.initComponent.call(this);

	}
});

//FsbsQmCrif DetailWin
FSBS.FsbsQmCrif.EditWin = Ext.extend(Infrastructure.BaseWindow, {
	title: '汽贸-商品车基本信息',
	iconCls: 'car'
});

// 二维码打印DataView
FSBS.FsbsQmCrif.QrCodeDataView = Ext.extend(Ext.DataView, {
	autoHeight: true,
	multiSelect: true,
	overClass: 'x-view-over',
	itemSelector: 'div.thumb-wrap',
	emptyText: 'No images to display',
	store: new FSBS.FsbsQmCrif.JsonStore(),

	plugins: [
		new Ext.DataView.DragSelector(),
		new Ext.DataView.LabelEditor({ dataIndex: 'name' })
	],

	initComponent: function () {
		Ext.applyIf(this, {
			tpl: new Ext.XTemplate(
				'<tpl for="."><div class="thumb-wrap" id="{Id}">',
				'<div class="thumb"><img width="150" height="150" src="/FsbsQmCrif/GetCarQrCodeById?size=150&Id={Id}"></div>',
				'<span> [品牌]:{Brand}</span></div>',
				'</tpl>',
				'<div class="x-clear"></div>'
			)
		});

		FSBS.FsbsQmCrif.QrCodeDataView.superclass.initComponent.call(this);
	}
});